package com.reading.common.utils.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author guwenqi_1995@163.com
 * @date 2019/7/10 10:42 AM
 **/
public class ValidatorImpl implements ConstraintValidator<Validator, Object> {

    private String values;

    @Override
    public void initialize(Validator validator) {
        this.values = validator.values();
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        //分割定义的有效值
        String[] value_array = values.split(",");
        boolean isFlag = false;
        //遍历比对有效值
        for (int i =0;i<value_array.length;i++)
        {
            //存在一致跳出循环，赋值isFlag=true
            if(value_array[i].equals(o))
            {
                isFlag = true;
                break;
            }
        }
        //返回是否存在boolean
        return isFlag;
    }

}
